/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ships.core.icc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import ships.core.icc.IccDefs.icValidateStatus;
import ships.core.icc.IccIO.CIccFileIO;
import ships.core.icc.IccIO.CIccIO;
import ships.core.icc.IccIO.CIccMemIO;
import ships.core.icc.IccIO.icSeekVal;

import ships.core.icc.IccTagBasic.CIccTag;
import ships.core.icc.icProfileHeader.icHeader;
import ships.core.icc.icProfileHeader.icProfileID;
import ships.core.icc.icProfileHeader.icTag;
import ships.core.icc.icProfileHeader.icTagSignature;
import ships.core.icc.icProfileHeader.icTagTypeSignature;

/**
 *
 * @author fqhuy
 */
public class IccProfile_ {

    public static enum icProfileIDSaveMethod {

        icVersionBasedID,
        icAlwaysWriteID,
        icNeverWriteID,
    }

    public static class IccTagEntry {

        public IccTagEntry() {
            //TagInfo =

        }

        icTag TagInfo = new icTag();
        CIccTag pTag;
    }

    public static class CIccProfile {

        public CIccProfile() {
            m_Header = new icProfileHeader.icHeader();
            m_Tags = new ArrayList<IccTagEntry>();
            m_pAttachIO = null;
            m_TagVals = new ArrayList<CIccTag>();
        }

        public CIccProfile(CIccProfile Profile) {
        }

        public CIccProfile assigns(CIccProfile Profile) {
            throw new UnsupportedOperationException();
        }

        public CIccTag FindTag(int sig) {
            throw new UnsupportedOperationException();
        }

        public boolean AttachTag(int sig, CIccTag pTag) {
            throw new UnsupportedOperationException();
        }

        public boolean DeleteTag(int sig) {
            throw new UnsupportedOperationException();
        }

        public CIccMemIO GetTagIO(int sig) {
            throw new UnsupportedOperationException();
        } //caller should delete returned result

        public boolean ReadTags(CIccProfile pProfile) {
            throw new UnsupportedOperationException();
        } // will read in all the tags using the IO of the passed profile

        public boolean Attach(CIccIO pIO) throws IOException{
            if (m_Tags.isEmpty()) {
                Cleanup();
            }

            if (!ReadBasic(pIO)) {
                Cleanup();
                return false;
            }

            m_pAttachIO = pIO;
            //throw new UnsupportedOperationException();
            return true;
        }

        public boolean Detach() {
            throw new UnsupportedOperationException();
        }

        public boolean Read(CIccIO pIO) {
            throw new UnsupportedOperationException();
        }

        public icValidateStatus ReadValidate(CIccIO pIO, String sReport) {
            throw new UnsupportedOperationException();
        }

        public boolean Write(CIccIO pIO, icProfileIDSaveMethod nWriteId) {
            throw new UnsupportedOperationException();
        }

        public void InitHeader() {
            throw new UnsupportedOperationException();
        }

        public icValidateStatus Validate(String sReport) {
            throw new UnsupportedOperationException();
        }

        public int GetSpaceSamples() {
            throw new UnsupportedOperationException();
        }

        public boolean AreTagsUnique() {
            throw new UnsupportedOperationException();
        }
        // public boolean IsTagPresent(int sig) { return (GetTag(sig)!=null);}

        protected void Cleanup() {

           // m_pAttachIO.
            //throw new UnsupportedOperationException();
        }

        protected IccTagEntry GetTag(int sig) {
            throw new UnsupportedOperationException();
        }

        protected IccTagEntry GetTag(CIccTag pTag) {
            throw new UnsupportedOperationException();
        }

        protected boolean ReadBasic(CIccIO pIO) throws IOException {
            m_Header.size = pIO.Read32(1)[0];
            m_Header.cmmId = pIO.Read32(1)[0];
            m_Header.version = pIO.Read32(1)[0];
            m_Header.deviceClass = icProfileHeader.icProfileClassSignature.fromInt(pIO.Read32(1)[0]);
            m_Header.colorSpace = icProfileHeader.icColorSpaceSignature.fromInt(pIO.Read32(1)[0]);
            m_Header.pcs = icProfileHeader.icColorSpaceSignature.fromInt(pIO.Read32(1)[0]);
            m_Header.date.year = pIO.Read16(1)[0];
            m_Header.date.month = pIO.Read16(1)[0];
            m_Header.date.day = pIO.Read16(1)[0];
            m_Header.date.hours = pIO.Read16(1)[0];
            m_Header.date.minutes = pIO.Read16(1)[0];
            m_Header.date.seconds = pIO.Read16(1)[0];
            m_Header.magic = pIO.Read32(1)[0];
            m_Header.platform = icProfileHeader.icPlatformSignature.fromInt(pIO.Read32(1)[0]);
            m_Header.flags = pIO.Read32(1)[0];
            m_Header.manufacturer = pIO.Read32(1)[0];
            m_Header.model = pIO.Read32(1)[0];
            m_Header.attributes = pIO.Read64(1)[0];
            m_Header.renderingIntent = pIO.Read32(1)[0];
            m_Header.illuminant.X = pIO.Read32(1)[0];
            m_Header.illuminant.Y = pIO.Read32(1)[0];
            m_Header.illuminant.Z = pIO.Read32(1)[0];
            m_Header.creator = pIO.Read32(1)[0];
            m_Header.profileID = new icProfileHeader.icProfileID(pIO.Read8(icProfileHeader.icProfileID.length));
            m_Header.reserved = pIO.Read8(m_Header.reserved.length);

            if (m_Header.magic != icProfileHeader.icMagicNumber) {
                return false;
            }

            int count = 0;
            IccTagEntry TagEntry = new IccTagEntry();

            TagEntry.pTag = null;

            count = pIO.Read32(1)[0];

            //Read TagDir
            for (int i = 0; i < count; i++) {
                TagEntry.TagInfo.sig = icProfileHeader.icTagSignature.fromInt(pIO.Read32(1)[0]);
                TagEntry.TagInfo.offset = pIO.Read32(1)[0];
                TagEntry.TagInfo.size = pIO.Read32(1)[0];
                m_Tags.add(TagEntry);
            }

            return true;
        }

        protected boolean LoadTag(IccTagEntry pTagEntry, CIccIO pIO) {
            throw new UnsupportedOperationException();
        }

        protected boolean DetachTag(CIccTag pTag) {
            throw new UnsupportedOperationException();
        }

        // Profile Validation functions
        protected icValidateStatus CheckRequiredTags(String sReport) {
            throw new UnsupportedOperationException();
        }

        protected boolean CheckTagExclusion(String sReport) {
            throw new UnsupportedOperationException();
        }

        protected icValidateStatus CheckHeader(String sReport) {
            throw new UnsupportedOperationException();
        }

        protected icValidateStatus CheckTagTypes(String sReport) {
            throw new UnsupportedOperationException();
        }

        protected boolean IsTypeValid(icTagSignature tagSig, icTagTypeSignature typeSig) {
            throw new UnsupportedOperationException();
        }

        protected boolean CheckFileSize(CIccIO pIO) {
            throw new UnsupportedOperationException();
        }
        protected icHeader m_Header;
        protected List<IccTagEntry> m_Tags;
        protected CIccIO m_pAttachIO;
        protected List<CIccTag> m_TagVals;
    }

    public static CIccProfile ReadIccProfile(String szFilename) {
        throw new UnsupportedOperationException();
    }

    public static CIccProfile ReadIccProfile(byte[] pMem, int nSize) {
        throw new UnsupportedOperationException();
    }

    public static CIccProfile OpenIccProfile(String szFilename) throws IOException{
        CIccFileIO pFileIO = new CIccFileIO();

        if (!pFileIO.Open(szFilename, "rb")) {
            //delete pFileIO;
            return null;
        }

        CIccProfile pIcc = new CIccProfile();

        if (!pIcc.Attach(pFileIO)) {
            //delete pIcc;
            //delete pFileIO;
            return null;
        }
        
        return pIcc;
        //throw new UnsupportedOperationException();
    }

    public static CIccProfile OpenIccProfile(byte[] pMem, int nSize) {
        throw new UnsupportedOperationException();
    }

    public static CIccProfile ValidateIccProfile(CIccIO pIO, String sReport, icValidateStatus nStatus) {
        throw new UnsupportedOperationException();
    }

    public static CIccProfile ValidateIccProfile(String szFilename, String sReport, icValidateStatus nStatus) {
        throw new UnsupportedOperationException();
    }

    public static boolean SaveIccProfile(String szFilename, CIccProfile pIcc, icProfileIDSaveMethod nWriteId) {
        throw new UnsupportedOperationException();
    }

    public static void CalcProfileID(CIccIO pIO, icProfileID profileID) {
        throw new UnsupportedOperationException();
    }

    public static boolean CalcProfileID(String szFilename, icProfileID profileID) {
        throw new UnsupportedOperationException();
    }
}
